#include <iostream>       // std::cout
#include <atomic>         // std::atomic, std::memory_order_relaxed
#include <thread>         // std::thread

// 原子变量，可用于多线程变量的同步
std::atomic<int> foo (0); // 声明一个原子变量 int foo = 0

void add_foo(int n) 
{
  int i;

  for (i = 0; i < n; i++)
      foo++;
}

int main ()
{
  std::thread th1(add_foo, 100);
  std::thread th2(add_foo, 100);
  std::thread th3(add_foo, 100);
  std::thread th4(add_foo, 100);
  
  th1.join();
  th2.join();
  th3.join();
  th4.join();

  std::cout << foo << std::endl;

  return 0;
}
